package com.song.osclient.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;

import com.song.osclient.R;
import com.song.osclient.bean.MainTab;
import com.song.osclient.dialog.QuickOptionDialog;
import com.song.osclient.view.CustomFragmentTabHost;


public class MainFragment extends Fragment {
    QuickOptionDialog quickOptionDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_main, null);
        setUpTabHost(view);
        initView(view);
        return view;
    }

    private void initView(View view) {
        quickOptionDialog = new QuickOptionDialog(getActivity());
        view.findViewById(R.id.btn_quick_option).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleQuickOptionDialog();
            }
        });
    }

    private void setUpTabHost(View view) {
        CustomFragmentTabHost tabHost = (CustomFragmentTabHost) view.findViewById(R.id.tab);
        tabHost.setup(getActivity(), getFragmentManager(), R.id.tabContent);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB)
            tabHost.getTabWidget().setShowDividers(0);
        for (MainTab tab : MainTab.tabs) {
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(tab.getTag());
            View tabView = View.inflate(getActivity(), R.layout.tab_indicator, null);
            TextView textView = (TextView) tabView.findViewById(R.id.tab_title);
            textView.setText(tab.getTitle());
            textView.setCompoundDrawablesWithIntrinsicBounds(0, tab.getDrawable(), 0, 0);
            tabSpec.setIndicator(tabView);
            if (tab.getIndex() == 2) {
                tabView.setVisibility(View.INVISIBLE);
            }
            tabHost.addTab(tabSpec, tab.getContentClass(), null);
        }
    }

    private void toggleQuickOptionDialog() {
        if (quickOptionDialog != null && !quickOptionDialog.isShowing()) {
            quickOptionDialog.show();
        } else if (quickOptionDialog != null) {
            quickOptionDialog.dismiss();
        }
    }

}
